//
//  StaffView.m
//  TigerClef
//
//  Created by Christopher Laguna on 6/28/13.
//  Copyright (c) 2013 Christopher Laguna. All rights reserved.
//

#import "StaffView.h"
#import "Note.h"


@implementation StaffView

@synthesize bottomLineYPos;
@synthesize staffLineStep;
@synthesize curNote;
@synthesize currentClef;
@synthesize otherView;


- (id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    self.layer.borderColor = [UIColor blackColor].CGColor;
    self.backgroundColor = [[UIColor alloc] initWithRed:1 green:1 blue:1 alpha:1.0];
    self.layer.borderWidth = 3.0f;
    
    //treble clef
    currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"TrebleClefImg.png"]];
    currentClef.frame = CGRectMake(-10, 10, 100, 100);
    [self addSubview:currentClef];
    
    UIImageView *staffIV = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"StaffLines.png"]];
    
    staffIV.frame = CGRectMake(8, 20, self.frame.size.width - 16, 67);
    
    [self addSubview:staffIV];
    
    bottomLineYPos = 87;
    staffLineStep = 8;
    /*
     self.endOfStaffIV = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"EndOfStaff.png"]];
     self.endOfStaffIV.center = CGPointMake(self.length - 8,self.middleLineY);
     [self addSubview:self.endOfStaffIV];
     */
    
    return self;
}

-(void) changeClef: (int) type {
    [currentClef removeFromSuperview];
    
    switch (type) {
        case kTreble: {
            currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"TrebleClefImg.png"]];
            currentClef.frame = CGRectMake(-10, 10, 100, 100);
            [self addSubview:currentClef];
            break;
        }
        case kBass: {
            currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"BassClefImg.png"]];
            currentClef.frame = CGRectMake(-10, -10, 120, 145);
            [self addSubview:currentClef];
            break;
        }
        case kAlto: {
            currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"AltoClefImg.png"]];
            currentClef.frame = CGRectMake(0, 20, 65, 68);
            [self addSubview:currentClef];
            break;
        }
        case kTenor: {
            currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"AltoClefImg.png"]];
            currentClef.frame = CGRectMake(0, 8, 65, 58);
            [self addSubview:currentClef];
            break;
        }
        case kFancy: {
            currentClef = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"mensuralCClef.png"]];
            currentClef.frame = CGRectMake(0, 14.5, 80, 110);
            [self addSubview:currentClef];
            break;
        }
    }
}

-(void) addNote: (int) type atPitch: (int) pitch {
    [self removeNote:curNote];
    
    Note *newNote = [[Note alloc] init:kQuarter];
    if(pitch > 3) {
        [newNote makeOriented: stemDown];
    }
    else {
        [newNote makeOriented: stemUp];
    }
    [newNote putCenterAt:CGPointMake(90, bottomLineYPos - (pitch * staffLineStep))];
    
    [self addSubview:newNote.imageView];
    curNote = newNote;
    
    if (pitch < 0) {
        int numledgerlines = (0 - pitch)/2;
        for(int i = 0; i < numledgerlines; i++) {
            UIImageView *newLedgerLine = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"LedgerLine.png"] ];
            [self addSubview: newLedgerLine];
            newLedgerLine.center = CGPointMake(curNote.imageView.center.x + curNote.xOffsetFromCenter - 2,
                                               bottomLineYPos + ((i + 1) * staffLineStep * 2));
            [curNote.ledgerlines addObject: newLedgerLine];
        }
    }
    
    if (pitch > 8) {
        int numledgerlines = (pitch - 8)/2;
        for(int i = 0; i < numledgerlines; i++) {
            UIImageView *newLedgerLine = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"LedgerLine.png"] ];
            [self addSubview: newLedgerLine];
            newLedgerLine.center = CGPointMake(curNote.imageView.center.x + curNote.xOffsetFromCenter + 2,
                                               bottomLineYPos - (staffLineStep * 8) - ((i + 1) * staffLineStep * 2));
            [curNote.ledgerlines addObject: newLedgerLine];
        }
    }
}

-(void) addOtherNote:(int) pitch {
    [self removeOtherNote];
    otherView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"BlackSquare.png"]];
    otherView.frame = CGRectMake(50,50 - (staffLineStep * pitch),75,75);
    [self addSubview:otherView];
}

-(void) removeNote: (Note *) note {
    [note.imageView removeFromSuperview];
    for (UIImageView *imageView in note.ledgerlines) {
        [imageView removeFromSuperview];
    }
}

-(void) removeOtherNote {
    [otherView removeFromSuperview];
}



@end
